#!/bin/bash

function OSConfigure(){

    echo 'net.ipv6.conf.all.disable_ipv6=1
    net.ipv6.conf.default.disable_ipv6=1
    net.ipv6.conf.lo.disable_ipv6=1
    net.ipv4.ip_forward=1
    net.ipv4.conf.all.forwarding=1
    fs.file-max=2097152
    vm.swappiness=0
    net.ipv4.ip_local_port_range = 4096 65000
    vm.overcommit_memory = 0
    vm.panic_on_oom = 0
    net.ipv4.tcp_max_syn_backlog = 262144 
    net.core.netdev_max_backlog = 262144
    net.ipv4.tcp_max_orphans = 262144
    net.ipv4.tcp_synack_retries= 1
    net.ipv4.tcp_syn_retries= 1
    net.ipv4.tcp_tw_reuse= 1' >> /etc/sysctl.conf

    sysctl -p


    echo 'br_netfilter
    ip6_udp_tunnel
    ip_set
    ip_set_hash_ip
    ip_set_hash_net
    iptable_filter
    iptable_nat
    iptable_mangle
    iptable_raw
    nf_conntrack_netlink
    nf_conntrack
    nf_conntrack_ipv4
    nf_defrag_ipv4
    nf_nat
    nf_nat_ipv4
    nf_nat_masquerade_ipv4
    nfnetlink
    udp_tunnel
    veth
    vxlan
    x_tables
    xt_addrtype
    xt_conntrack
    xt_comment
    xt_mark
    xt_multiport
    xt_nat
    xt_recent
    xt_set
    xt_statistic' > /etc/modules-load.d/rancher_k8s.conf

    echo '* soft nofile 655350
    * hard nofile 655350
    * soft nproc unlimited
    * hard nproc unlimited' >> /etc/security/limits.conf
}

function InstallDocker(){
    mkdir /etc/docker
    echo '{
        "oom-score-adjust": -1000,
        "max-concurrent-downloads": 10,
        "max-concurrent-uploads": 10,
        "bip": "192.168.1.1/24",
        "live-restore": true,
        "registry-mirrors": ["https://7bezldxe.mirror.aliyuncs.com"],
        "storage-opts": [
        "overlay2.override_kernel_check=true"
        ]
    }' > /etc/docker/daemon.json

    yum_utils_install=$(sudo yum install -y yum-utils device-mapper-persistent-data lvm2)

    sleep 10

    add_repo=$(sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo)
    
    sleep 5

    yum install -y docker-ce docker-ce-cli containerd.io

    sudo systemctl start docker.service && sudo systemctl enable docker.service
    useradd -g docker rancher
}

function BinInstall(){
    yum install -y targetd targetcli git
}

OSConfigure
InstallDocker
BinInstall